#
# SYNOPSIS
#
#   AX_PROTOBUF([yes|no|auto])
#
# DESCRIPTION
#
#   Searches for google's 'protocole buffer' library with the --with... option.
#
#   If found, define HAVE_PROTOBUF and macros PROTOC, PROTOBUF_LIBS and PROTOBUF_CXXFLAGS.
#
#   The argument is used if no --[en|dis]able-protobuf option is set. Value "yes"
#   requires the configuration by default. Value "no" does not require it by
#   default. Value "auto" configures the library only if available.
#
# LAST MODIFICATION
#
#   2008-09-08
#
# COPYLEFT
#
#   Copyright (c) 2008 Ivan Topolsky <ivan.topolsky@genebio.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU Library General Public License as
#   published by the Free Software Foundation; either version 2 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Library General Public
#   License along with this program; if not, write to the
#   Free Software Foundation, Inc.,
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

# AX_PROTOBUF([yes|no|auto])
AC_DEFUN([AX_PROTOBUF],[

  # enable / disable using 
  AC_MSG_CHECKING([whether google protocole buffer is enabled])
  AC_ARG_ENABLE([protobuf],[AC_HELP_STRING(
    [--enable-protobuf],[enable using google protocole buffer (default:autodetect)])
AC_HELP_STRING(
    [--disable-protobuf],[disable using google protocole buffer])],
  [
    AC_MSG_RESULT([$enableval])
    ac_with_protobuf=$enableval
  ],[
    AC_MSG_RESULT([$1])
    ac_with_protobuf=$1
  ])

  # path to includes
  AC_ARG_WITH([protobuf-includes],[AC_HELP_STRING(
    [--with-protobuf-includes=<dir>],
    [include path for google protocole buffer])],
  [
    ac_with_protobuf_includes="-I$withval"
  ])

  # path to libs
  AC_ARG_WITH([protobuf-libs],[AC_HELP_STRING(
   [--with-protobuf-libs=<dir>],
   [library path for google protocole buffer])],
  [
    ac_with_protobuf_libs="-L$withval"
  ])


  if test "$ac_with_protobuf" = "yes" -o "$ac_with_protobuf" = "auto" ; then

    # step 1 : find compiler
    AC_MSG_CHECKING([path for protoc])
    if PROTOC=`which protoc 2> /dev/null`; then 
      AC_MSG_RESULT([$PROTOC])

      # step 2 : find includes
      oldCXXFLAGS="$CXXFLAGS"
      PROTOBUF_CXXFLAGS="$ac_with_protobuf_includes"
      CXXFLAGS="$oldCXXFLAGS $PROTOBUF_CXXFLAGS"
      AC_CHECK_HEADERS([google/protobuf/stubs/common.h],[

        # step 3 : find libs
        oldLIBS="$LIBS"
        PROTOBUF_LIBS="$ac_with_protobuf_libs -lprotobuf"
        LIBS="$oldLD $PROTOBUF_LIBS"
        AC_MSG_CHECKING([for google::protobuf::LogSilencer in library protobuf])
        AC_LINK_IFELSE([AC_LANG_PROGRAM([#include "google/protobuf/stubs/common.h"],[new class google::protobuf::LogSilencer()])],[
          AC_MSG_RESULT([yes])

          # everything is OK !
          AC_DEFINE([HAVE_PROTOBUF],[1],[Google protocole buffer library is available])
          HAVE_PROTOBUF=1
          AC_SUBST([HAVE_PROTOBUF])
          AC_SUBST([PROTOC])
          AC_SUBST([PROTOBUF_LIBS])
          AC_SUBST([PROTOBUF_CXXFLAGS])
        ],[
          AC_MSG_RESULT([no])
        ])
        LIBS="$oldLIBS"
      ])
      CXXFLAGS="$oldCXXFLAGS"
    else
      AC_MSG_RESULT([not found])
    fi

    if test "$ac_with_protobuf" = "yes" -a "x$HAVE_PROTOBUF" = "x" ; then
      AC_MSG_ERROR([cannot configure required google protocole buffer.
Please try to specify location using '--with-protobuf-includes' and '--with-protobuf-libs'])
    fi
  fi
])
